package com.hepinghospital.user.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.shape.view.ShapeTextView;
import com.hepinghospital.user.R;
import com.rest.response.Doctor;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import jz.joyoung.robot.util.TimeUtil;


/**
 * @author HP
 * @date 2020/3/2
 */

public class GeneralScheduleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<Doctor.GeneralSchedule> list;
    private OnItemClickLitener mOnItemClickLitener;

    public GeneralScheduleAdapter(Context context, List<Doctor.GeneralSchedule> list) {
        this.context = context;
        this.list = list;
    }


    @NotNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        View view = LayoutInflater.from(context)
                .inflate(R.layout.item_general_schedule, parent, false);
        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NotNull RecyclerView.ViewHolder holder, final int position) {
        final ViewHolder myHolder = (ViewHolder) holder;
        myHolder.tvScheduleTime.setText(TimeUtil.getWeekOfDate(list.get(position).scheduledata, list.get(position).timeInterval));
        myHolder.tvDate.setText(list.get(position).scheduledata.substring(5));
        myHolder.tvDeptName.setText(list.get(position).deptName);
        if (list.get(position).isSchedule == 0) {
            myHolder.btnConfirm.setText("已约满");
            myHolder.btnConfirm.getShapeDrawableBuilder().setSolidColor(ContextCompat.getColor(context, R.color.gray_f2)).intoBackground();
            myHolder.btnConfirm.getTextColorBuilder().setTextColor(ContextCompat.getColor(context, R.color.commonGrey)).intoTextColor();
        } else {
            myHolder.btnConfirm.setText("预约");
            myHolder.btnConfirm.getShapeDrawableBuilder().setSolidColor(ContextCompat.getColor(context, R.color.color_4d7cfa)).intoBackground();
            myHolder.btnConfirm.getTextColorBuilder().setTextColor(ContextCompat.getColor(context, R.color.white));
        }
        if (mOnItemClickLitener != null) {
            myHolder.btnConfirm.setOnClickListener(v -> mOnItemClickLitener.onItemClick(myHolder.btnConfirm, position));
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);
    }


    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_scheduleTime)
        TextView tvScheduleTime;
        @BindView(R.id.tv_date)
        TextView tvDate;
        @BindView(R.id.tv_dept_name)
        TextView tvDeptName;
        @BindView(R.id.btn_confirm)
        ShapeTextView btnConfirm;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
